import type { UserInfo, UserQuery, PageResult, ApiResponse, ResetPasswordParams, AssignRoleParams } from '@/types/user.type'
import { UserStatus } from '@/types/user.type'

// 模拟用户数据
const mockUsers: UserInfo[] = [
  {
    id: 1,
    username: 'admin',
    realName: '管理员',
    email: 'admin@example.com',
    phone: '13800138000',
    avatar: '',
    status: UserStatus.ENABLED,
    deptId: 1,
    roles: [
      { id: 1, roleName: '超级管理员', roleCode: 'SUPER_ADMIN' }
    ],
    createTime: '2024-01-01 00:00:00',
    lastLoginTime: '2024-03-15 10:00:00',
    updateTime: '2024-01-01 12:00:00'
  },
  {
    id: 2,
    username: 'test',
    realName: '测试用户',
    email: 'test@example.com',
    phone: '13800138001',
    avatar: '',
    status: UserStatus.ENABLED,
    deptId: 2,
    roles: [
      { id: 2, roleName: '普通用户', roleCode: 'USER' }
    ],
    createTime: '2024-01-02 00:00:00',
    lastLoginTime: '2024-03-14 15:30:00',
    updateTime: '2024-01-01 12:00:00'
  },
  {
    id: 3,
    username: 'disabled',
    realName: '禁用用户',
    email: 'disabled@example.com',
    phone: '13800138002',
    avatar: '',
    status: UserStatus.DISABLED,
    deptId: 2,
    roles: [
      { id: 2, roleName: '普通用户', roleCode: 'USER' }
    ],
    createTime: '2024-01-03 00:00:00',
    lastLoginTime: '2024-03-13 09:15:00',
    updateTime: '2024-01-01 12:00:00'
  }
]

// 模拟角色数据
const mockRoles = [
  { id: 1, roleName: '超级管理员', roleCode: 'SUPER_ADMIN' },
  { id: 2, roleName: '普通用户', roleCode: 'USER' }
]

// 模拟token
const tokens = new Map<string, string>()

// 模拟登录
export function mockLogin(username: string, password: string) {
  const user = mockUsers.find(u => u.username === username)
  if (!user || password !== '123456') {
    return {
      code: 1,
      message: '用户名或密码错误',
      data: null
    }
  }
  return {
    code: 0,
    message: '登录成功',
    data: {
      token: `mock-token-${user.id}`,
      userInfo: user
    }
  }
}

// 模拟获取用户信息
export function mockGetUserInfo(token: string) {
  const userId = parseInt(token.split('-')[2])
  const user = mockUsers.find(u => u.id === userId)
  if (!user) {
    return {
      code: 1,
      message: '用户不存在',
      data: null
    }
  }
  return {
    code: 0,
    message: '获取成功',
    data: user
  }
}

// 模拟登出
export function mockLogout(token: string) {
  tokens.delete(token)
  return {
    code: 0,
    data: null,
    message: '登出成功'
  }
}

// 模拟修改密码
export function mockChangePassword(token: string, oldPassword: string, newPassword: string) {
  const userId = parseInt(token.split('-')[2])
  const user = mockUsers.find(u => u.id === userId)
  if (!user || oldPassword !== '123456') {
    return {
      code: 1,
      message: '原密码错误',
      data: null
    }
  }
  return {
    code: 0,
    message: '修改成功',
    data: null
  }
}

// 获取用户列表
export function mockGetUserList(params: UserQuery): Promise<ApiResponse<UserInfo[]>> {
  const { username, realName, phone, status } = params

  let filteredList = [...mockUsers]

  if (username) {
    filteredList = filteredList.filter(item => item.username.includes(username))
  }
  if (realName) {
    filteredList = filteredList.filter(item => item.realName.includes(realName))
  }
  if (phone) {
    filteredList = filteredList.filter(item => item.phone.includes(phone))
  }
  if (status !== undefined) {
    filteredList = filteredList.filter(item => item.status === status)
  }

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: filteredList
      })
    }, 500)
  })
}

// 获取用户详情
export function mockGetUserById(id: number): ApiResponse<UserInfo> {
  const user = mockUsers.find(u => u.id === id)
  if (!user) {
    return {
      code: 1,
      msg: '用户不存在',
      data: null
    }
  }
  return {
    code: 0,
    msg: '获取成功',
    data: user
  }
}

// 创建用户
export function mockCreateUser(data: Omit<UserInfo, 'id'>): Promise<ApiResponse<number>> {
  const newId = Math.max(...mockUsers.map(item => item.id)) + 1
  const newUser: UserInfo = {
    ...data,
    id: newId,
    createTime: new Date().toISOString().replace('T', ' ').slice(0, 19),
    updateTime: new Date().toISOString().replace('T', ' ').slice(0, 19)
  }
  mockUsers.push(newUser)

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: newId
      })
    }, 500)
  })
}

// 更新用户
export function mockUpdateUser(data: UserInfo): Promise<ApiResponse<void>> {
  const index = mockUsers.findIndex(item => item.id === data.id)
  if (index === -1) {
    return Promise.reject({
      code: 404,
      message: '用户不存在'
    })
  }

  mockUsers[index] = {
    ...data,
    updateTime: new Date().toISOString().replace('T', ' ').slice(0, 19)
  }

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: undefined
      })
    }, 500)
  })
}

// 删除用户
export function mockDeleteUser(id: number): Promise<ApiResponse<void>> {
  const index = mockUsers.findIndex(item => item.id === id)
  if (index === -1) {
    return Promise.reject({
      code: 404,
      message: '用户不存在'
    })
  }

  mockUsers.splice(index, 1)

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: undefined
      })
    }, 500)
  })
}

// 重置密码
export function mockResetPassword(data: ResetPasswordParams): Promise<ApiResponse<void>> {
  const index = mockUsers.findIndex(item => item.id === data.userId)
  if (index === -1) {
    return Promise.reject({
      code: 404,
      message: '用户不存在'
    })
  }

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: undefined
      })
    }, 500)
  })
}

// 分配角色
export function mockAssignRoles(data: AssignRoleParams): Promise<ApiResponse<void>> {
  const index = mockUsers.findIndex(item => item.id === data.userId)
  if (index === -1) {
    return Promise.reject({
      code: 404,
      message: '用户不存在'
    })
  }

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: undefined
      })
    }, 500)
  })
} 